package com.xdz.coupon.service.impl;

import com.xdz.coupon.entity.MemberPriceEntity;
import com.xdz.coupon.entity.SkuLadderEntity;
import com.xdz.coupon.service.MemberPriceService;
import com.xdz.coupon.service.SkuLadderService;
import com.xdz.to.MemberPrice;
import com.xdz.to.SkuReduceTo;
import com.xdz.utils.PageUtils;
import com.xdz.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xdz.coupon.dao.SkuFullReductionDao;
import com.xdz.coupon.entity.SkuFullReductionEntity;
import com.xdz.coupon.service.SkuFullReductionService;
import org.springframework.util.CollectionUtils;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReductioneInfo(SkuReduceTo skuReduceTo) {
        Long skuId = skuReduceTo.getSkuId();
        // 保存满减优惠信息
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        skuFullReductionEntity.setSkuId(skuId);
        skuFullReductionEntity.setFullPrice(skuReduceTo.getFullPrice());
        skuFullReductionEntity.setReducePrice(skuReduceTo.getReducePrice());
        skuFullReductionEntity.setAddOther(skuReduceTo.getPriceStatus());
        this.save(skuFullReductionEntity);
        // 保存满件打折信息
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuId);
        skuLadderEntity.setDiscount(skuReduceTo.getDiscount());
        skuLadderEntity.setAddOther(skuReduceTo.getCountStatus());
        skuLadderEntity.setFullCount(skuReduceTo.getFullCount());
        skuLadderService.save(skuLadderEntity);
        // 保存会员价格信息
        List<MemberPrice> memberPrice = skuReduceTo.getMemberPrice();
        if (CollectionUtils.isEmpty(memberPrice)) {
            return;
        }
        List<MemberPriceEntity> memberPriceEntities = memberPrice.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuId);
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }

}